package com.example.financialsystem;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.financialsystem.adapter.RecyclerStatisticsAdapter;
import com.example.financialsystem.custom.InviteMemberDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import models.MemberInfo;
import models.StatisticInfo;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import others.ServerResponse;

import static others.Const.IP;
import static others.Const.PORT;
import static others.Const.currentBook;
import static others.Const.currentIdAndNameHashTable;
import static others.Const.currentUserVo;
import static others.Const.isVisible;
import static others.Tools.ToastShow;
import static utils.GsonUtil.Json2Class;
import static utils.GsonUtil.String2ListMemberInfo;
import static utils.GsonUtil.String2ListStatisticServerResponse;

public class BookManagementActivity extends AppCompatActivity {

    @BindView(R.id.book_management_invite)
    Button bookManagementInvite;
    @BindView(R.id.book_management_memberlist)
    ListView bookManagementMemberlist;
    @BindView(R.id.book_management_quit)
    Button bookManagementQuit;

    public String responseData;
    public static final int GET_MEMBERS = 1;
    private List<String> listData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_management);
        ButterKnife.bind(this);

        setGetMembers();
    }

    @OnClick(R.id.book_management_invite)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.book_management_invite:
                InviteMemberDialog inviteMemberDialog
                        = new InviteMemberDialog(this, R.style.MyDialog);
                inviteMemberDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                inviteMemberDialog.setCanceledOnTouchOutside(true);
                inviteMemberDialog.show();
                break;
        }
    }

    public void setGetMembers(){
        String url = "http://" + IP + ":" + PORT + "/portal/ownership/members.do";
        RequestBody requestBody = new FormBody.Builder()
                .add("bookId",currentBook.getId())
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        sendRequest(request,GET_MEMBERS);
    }

    public void sendRequest(Request request,int TAG){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();
                    Response response = client.newCall(request).execute();
                    responseData = response.body().string();
                    Message message = new Message();
                    message.what = TAG;
                    handler.sendMessage(message);
                } catch (Exception e){
                    e.printStackTrace();
                    ToastShow("我们可能遭遇了一些麻烦1(BookManagementActivity)");
                }
            }
        }).start();
    }

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case GET_MEMBERS:
                    Log.d("test", responseData);
                    ServerResponse<List<MemberInfo>> serverResponse = String2ListMemberInfo(responseData);
                    if (serverResponse.getStatus() == 0){
                        List<MemberInfo> members = serverResponse.getData();
                        for (int i=0;i<members.size();i++){
                            MemberInfo temp = members.get(i);
                            String name = currentIdAndNameHashTable.get(temp.getUserId());
                            listData.add(name + "   " + temp.getType());
                        }
                        String[] data = (String[])listData.toArray(new String[listData.size()]);
                        ArrayAdapter<String> array = new ArrayAdapter<String>(
                                BookManagementActivity.this,android.R.layout.simple_list_item_1,data);
                        bookManagementMemberlist.setAdapter(array);
                    } else {
                        ToastShow(serverResponse.getMsg()+"(BookManagementActivity)");
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
